local map = {
  ["unexpectation"] = "emo__canqing_damage-round",
  ["nullification"] = "emo__canqing_cancel-round",
  ["ex_nihilo"] = "emo__canqing_draw-round",
}

local skel = fk.CreateSkill {
  name = "emo__canqing",
  dynamic_desc = function (self, player, lang)
    local str = "emo__canqing_inner"
    for _, name in ipairs({"unexpectation", "ex_nihilo", "nullification"}) do
      local trans = Fk:translate("emo__canqing_" .. name, lang)
      local mark = map[name]
      local me = player:getMark(mark)
      local second -- 第二小的标记数
      for _, p in ipairs(Fk:currentRoom().alive_players) do
        if p ~= player then
          second = second and math.min(second, p:getMark(mark)) or p:getMark(mark)
        end
      end
      if not second then
        trans = trans .. "(√)"
      else
        trans = trans .. "(" .. me .. "/" .. second .. ")"
        if me > second then
          trans = "<font color=\'gray\'>".. trans .."</font>"
        end
      end
      str = str .. ":" .. trans
    end
    return str
  end,
}

Fk:loadTranslationTable{
  ["emo__canqing"] = "残擎",
  [":emo__canqing"] = "若你本轮以下值最少，则可以将一张红色牌当对应牌使用：造成伤害→【出其不意】；摸牌数→【无中生有】；抵消牌数→【无懈可击】。",
  [":emo__canqing_inner"] = "若你本轮以下值最少，则可以将一张红色牌当对应牌使用：{1}；{2}；{3}。",

  ["#emo__canqing"] = "残擎：转化一张红色牌",
  ["emo__canqing_unexpectation"] = "造成伤害→【出其不意】",
  ["emo__canqing_ex_nihilo"] = "摸牌数→【无中生有】",
  ["emo__canqing_nullification"] = "抵消牌数→【无懈可击】",
}

local canUseNames = function (player)
  local names = {}
  local players = Fk:currentRoom().alive_players
  for name, mark in pairs(map) do
    if table.every(players, function (p)
      return p:getMark(mark) >= player:getMark(mark)
    end) then
      table.insert(names, name)
    end
  end
  return names
end

skel:addEffect("viewas", {
  anim_type = "offensive",
  pattern = "unexpectation,ex_nihilo,nullification",
  prompt = "#emo__canqing",
  interaction = function(self, player)
    local all_choices = canUseNames(player)
    local choices = player:getViewAsCardNames(skel.name, all_choices)
    if #choices > 0 then
      return UI.CardNameBox { choices = choices, all_choices = all_choices }
    end
  end,
  handly_pile = true,
  filter_pattern = {
    min_num = 1, max_num = 1, pattern = ".|.|red",
  },
  view_as = function (self, player, cards)
    if #cards ~= 1 then return nil end
    local name = self.interaction.data
    if Fk.all_card_types[name] == nil then return end
    local c = Fk:cloneCard(name)
    c.skillName = skel.name
    c:addSubcard(cards[1])
    return c
  end,
  enabled_at_play = function(self, player)
    return #player:getViewAsCardNames(skel.name, canUseNames(player)) > 0
  end,
  enabled_at_response = function (self, player, response)
    return not response and not player:isNude()
    and #player:getViewAsCardNames(skel.name, canUseNames(player)) > 0
  end,
})

skel:addEffect(fk.Damage, {
  can_refresh = function (self, event, target, player, data)
    return target == player and not player.dead
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addPlayerMark(player, "emo__canqing_damage-round", 1)
  end,
})

-- 只有使用闪和无懈算抵消
skel:addEffect(fk.CardUseFinished, {
  can_refresh = function (self, event, target, player, data)
    return target == player and not player.dead
  end,
  on_refresh = function (self, event, target, player, data)
    local use = data.responseToEvent
    if use and use.isCancellOut then
      player.room:addPlayerMark(player, "emo__canqing_cancel-round", 1)
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    return player.seat == 1
  end,
  on_refresh = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.to and not move.to.dead and move.toArea == Card.PlayerHand and move.moveReason == fk.ReasonDraw then
        player.room:addPlayerMark(move.to, "emo__canqing_draw-round", #move.moveInfo)
      end
    end
  end,
})

return skel
